package dev.blogmoon.cn.admin.controller;

import dev.blogmoon.cn.common.entity.BatchRemoveReq;
import dev.blogmoon.cn.common.entity.PageResult;
import dev.blogmoon.cn.common.entity.Result;
import dev.blogmoon.cn.system.module.QueryDictReq;
import dev.blogmoon.cn.system.module.SysDictDTO;
import dev.blogmoon.cn.system.repository.SysDictRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/admin/dict")
public class SysDictController {

    @Autowired
    private SysDictRepository sysDictRepository;

    @PostMapping("/page")
    public Result page(@RequestBody QueryDictReq req) {
        PageResult<SysDictDTO> pageResult = sysDictRepository.page(req);
        return Result.success(pageResult);
    }

    @PostMapping("/save")
    public Result save(@RequestBody SysDictDTO sysDictDTO) {
        sysDictRepository.save(sysDictDTO);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam("id")Long id){
        SysDictDTO sysDictDTO = sysDictRepository.getById(id);
        return Result.success(sysDictDTO);
    }

    @PostMapping("/delete")
    public Result removeById(@RequestBody BatchRemoveReq req){
        sysDictRepository.remove(req.getIds());
        return Result.success();
    }

}
